import sys
import math
import pandas as pd

input_dir = sys.argv[1]

R = (1/4) * math.log((1 + 2*0.01) / (1 - 2*0.01)) * 100
df = pd.DataFrame()

for chr_id in range(1, 11):
    file_reader = open(f"{input_dir}/bin_out_{chr_id}.tsv", "r")
    lines = 0
    data = []
    bins = 0
    bin_data = []
    while lines < 3:
        line = file_reader.readline()
        line_str = line.strip()
        line_data = line_str.split(',')[1:]
        bins = len(line_data)
        data.append(line_data)
        lines+=1

    for i in range(bins):
        bin_data.append([data[0][i],data[1][i],data[2][i]])

    bin_df = pd.DataFrame(bin_data, columns=["Start", "End", "Center"])
    bin_df["CHR"] = chr_id
    df = pd.concat([df, bin_df])

df["Bin_name"] = [f"Bin{str(i+1).zfill(4)}" for i in range(len(df))]
df["Genetic_distance"] = df["Start"].apply(lambda x: int(x)/1000000*R)
df.to_csv(f"{input_dir}/bins.csv", index=False)
